<template>
  <div class="app-container org">
    <el-card class="leftCard">
      <!-- 搜索条件盒子 -->
      <div>
        <p class="pinformation">搜索条件</p>
        <el-form :model="queryParams" ref="queryRef" label-width="120px">
          <el-form-item label="检测项目名称：" prop="jcxmName">
            <el-input
              v-model="queryParams.jcxmName"
              placeholder="请输入检测项目名称"
              maxlength="40"
              clearable
              @keyup.enter="handleQuery"
            />
          </el-form-item>
          <el-row :gutter="15">
            <el-col :span="13">
              <el-form-item label="所属类别组：" prop="jclbId">
                <el-select
                  clearable
                  v-model="queryParams.jclbId"
                  placeholder="请选择所属类别组"
                  filterable
                  @change="handleQuery"
                >
                  <el-option
                    v-for="item in proxy.addListNoLimit(
                      jclbTypeList,
                      'pJclbId',
                      'jclbGroupName'
                    )"
                    :key="item.pJclbId"
                    :label="item.jclbGroupName"
                    :value="item.pJclbId"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="11">
              <el-row justify="end">
                <el-button type="success" @click="handleQuery">搜索</el-button>
                <el-button class="business resetBtn" @click="resetQuery"
                  >重置</el-button
                >
              </el-row>
            </el-col>
          </el-row>
        </el-form>
      </div>

      <div>
        <el-row>
          <p class="pinformation">检测项目列表</p>
          <p class="totalStyle">共 {{ total }} 条数据</p>
          <el-row style="padding-bottom: 5px; margin-left: 15px">
            <!-- <el-button
              link
              type="success"
              icon="Refresh"
              @click="handleSynchronous"
              :loading="loadingBtn"
              >同步检测项目</el-button
            > -->
          </el-row>
        </el-row>

        <el-table
          ref="mainTableRef"
          class="business"
          v-loading="loading"
          :row-class-name="tableRowClass"
          :data="jcxmTable"
          @row-click="rowClick"
        >
          <el-table-column
            label="类别组"
            align="center"
            prop="jclbName"
            width="250"
          />
          <el-table-column label="检测项目" align="center" prop="jcxmName">
            <template #default="scope">
              <span v-html="transformation(scope.row.jcxmName)"></span>
            </template>
          </el-table-column>
          <el-table-column
            label="国标编号"
            align="center"
            prop="jcxmCode"
            width="100"
          />
        </el-table>
        <pagination
          v-show="total > 0"
          :total="total"
          v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
        />
      </div>
    </el-card>

    <div class="rightCard">
      <el-card style="margin-bottom: 0.75vh">
        <!-- 基础信息 -->
        <el-form label-width="130px" :model="form" ref="jcxmRef">
          <div>
            <el-row style="margin-bottom: 5px">
              <el-col :span="22">
                <p class="pinformation">基础信息</p>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="7">
                <el-form-item label="所属类别组：" prop="jclbId">
                  {{
                    proxy.getItemByArray(
                      jclbTypeList,
                      "jclbId",
                      form.jclbId,
                      "jclbName"
                    ) || "/"
                  }}
                </el-form-item>
              </el-col>
              <el-col :span="7">
                <el-form-item label="检测项目名称：" prop="jcxmName">
                  <div v-if="!form.jcxmName">/</div>
                  <div v-html="transformation(form.jcxmName)" v-else></div>
                </el-form-item>
              </el-col>
              <el-col :span="7">
                <el-form-item label="别名：" prop="jcxmAliasName">
                  {{ form.jcxmAliasName || "/" }}
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="7">
                <el-form-item label="英文名称：" prop="jcxmEnglishName">
                  {{ form.jcxmEnglishName || "/" }}
                </el-form-item>
              </el-col>
              <el-col :span="7">
                <el-form-item label="CAS号：" prop="casName">
                  {{ form.casName || "/" }}
                </el-form-item>
              </el-col>
              <el-col :span="7">
                <el-form-item label="质量数：" prop="qualityCount">
                  {{ form.qualityCount || "/" }}
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="7">
                <el-form-item label="国标编号：" prop="jcxmCode">
                  {{ form.jcxmCode || "/" }}
                </el-form-item>
              </el-col>
              <el-col :span="7">
                <el-form-item label="所属国标文件：" prop="fileName">
                  {{ form.fileName || "/" }}
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="7">
                <el-form-item label="类别组内排序号：" prop="orderId">
                  {{ form.orderId || "/" }}
                </el-form-item>
              </el-col>
              <el-col :span="7">
                <el-form-item label="化学符号表示：" prop="jcxmChemName">
                  {{ form.jcxmChemName || "/" }}
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="化学符号：">
                  <div v-if="!form.jcxmChemName">/</div>
                  <div v-html="transformation(form.jcxmChemName)" v-else></div>
                </el-form-item>
              </el-col>
            </el-row>
          </div>
        </el-form>
      </el-card>
      <el-card>
        <el-tabs v-model="activeName" @tab-change="tabchange">
          <el-tab-pane label="分析方法" name="first">
            <div v-if="activeName == 'first'">
              <el-row style="justify-content: space-between">
                <el-row>
                  <p class="pinformation">分析方法列表</p>
                  <p class="totalStyle">
                    共
                    <template v-if="form.basicMethodList">
                      {{ form.basicMethodList.length }}
                    </template>
                    <template v-else>0</template> 条数据
                  </p>
                </el-row>
                <el-row justify="end">
                  <el-button type="success" size="small" @click="submit"
                    >保存</el-button
                  >
                </el-row>
              </el-row>
            </div>
            <el-table
              class="business"
              ref="subTableRef"
              :data="form.basicMethodList"
              border
            >
              <el-table-column
                label="方法编号"
                align="center"
                prop="methodCode"
                width="125"
              />
              <el-table-column
                label="方法名称"
                align="center"
                prop="methodName"
                width="400"
                show-overflow-tooltip
              />
              <el-table-column
                label="检测类别"
                align="center"
                width="160"
                prop="jclbNames"
                show-overflow-tooltip
              />
              <el-table-column label="组别" align="center" width="100">
                <template #default="scope">
                  {{
                    proxy.selectDictLabel(lims_jcxm_group, scope.row.groupId)
                  }}
                </template>
              </el-table-column>
              <el-table-column label="当前状态" align="center" prop="status" width="90">
                <template #default="scope">
                  <dict-tag
                    :options="lims_standard_status"
                    :value="scope.row.status"
                  />
                </template>
              </el-table-column>
              <el-table-column
                label="机构最小用量"
                align="center"
                prop="minUse"
              >
                <template #default="scope">
                  <el-input
                    v-model="scope.row.minUse"
                    placeholder="请输入"
                    clearable
                    oninput="value=value.replace(/[^0-9.]/g,'')"
                    @blur="scope.row.minUse = $event.target.value"
                  ></el-input>
                </template>
              </el-table-column>
              <el-table-column
                label="机构最小用量单位"
                align="center"
                prop="unitId"
                width="160"
              >
                <template #default="scope">
                  <el-select v-model="scope.row.unitId" filterable>
                    <el-option
                      v-for="item in unitList"
                      :key="item.unitId"
                      :value="item.unitId"
                      :label="item.unitName"
                    >
                      <span v-html="transformation(item.unitName)"></span>
                    </el-option>
                  </el-select>
                </template>
              </el-table-column>
              <el-table-column
                label="默认常用方法"
                align="center"
                prop="isOne"
                width="130"
              >
                <template #default="scope">
                  <el-select
                    v-model="scope.row.isOne"
                    @change="isOneChange(scope)"
                  >
                    <el-option
                      v-for="item in isOneOptions"
                      :key="item.authId"
                      :value="item.authId"
                      :label="item.authName"
                    />
                  </el-select>
                </template>
              </el-table-column>
            </el-table>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
  </div>
</template>

<script setup name="Jcxm">
import {
  listJcxm,
  listMethod,
  getJcxm,
  addJcxm,
  postJcxm,
  getJcxmStatus,
} from "@/api/store/jcxm";
import { allListJclbGroup } from "@/api/store/jclb";
import { getUnitList } from "@/api/store/unit";

const { proxy } = getCurrentInstance();
const { lims_jcxm_group, lims_standard_status } = proxy.useShareDict(
  "lims_jcxm_group",
  "lims_standard_status"
);

const jcxmList = ref([]);
const loading = ref(true);
const ids = ref([]);
const multiple = ref(false);
const disabled = ref(false);
const total = ref(0);
const jcxmTable = ref([]);
const jcxmForm = ref({});
const heightBright = ref("");
const activeName = ref("first");
const jclbTypeList = ref([]);
const unitList = ref([]);

// 默认常用方法
const isOneOptions = ref([
  { authId: 1, authName: "是" },
  { authId: 0, authName: "否" },
]);

const isUseList = ref([
  { useId: 1, useName: "是" },
  { useId: 0, useName: "否" },
]);

const isAuthList = ref([
  { useId: 1, useName: "已通过" },
  { useId: 0, useName: "未通过" },
]);

const data = reactive({
  form: {
    jcxmChemName: "",
  },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    jclbId: -1,
    jcxmCode: null,
    jcxmName: null,
    jcxmEnglishName: null,
    fileName: null,
    jcxmAliasName: null,
    groupId: null,
    jcxmChemName: null,
    casName: null,
    orderId: null,
    methodId: null,
    jcxmInstrumentName: null,
    createUserId: null,
    updateUserId: null,
  },
});

const { queryParams, form } = toRefs(data);

/** 查询检测项目列表 */
function getList() {
  loading.value = true;
  listJcxm(queryParams.value).then((response) => {
    jcxmTable.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}
// 获取检测类别组
function getJclbTypeList() {
  allListJclbGroup().then((res) => {
    jclbTypeList.value = res.data;
  });
}

getUnitList().then((res) => {
  unitList.value = res.data;
});

// 表单重置
function reset() {
  form.value = {
    jcxmId: null,
    jclbId: null,
    jcxmCode: null,
    jcxmName: null,
    jcxmEnglishName: null,
    fileName: null,
    jcxmAliasName: null,
    groupId: null,
    jcxmChemName: null,
    casName: null,
    orderId: null,
    methodId: null,
    jcxmInstrumentName: null,
    createUserId: null,
    createTime: null,
    updateUserId: null,
    updateTime: null,
    delFlag: null,
    remark: null,
  };
  proxy.resetForm("jcxmRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 修改按钮操作 */
async function rowClick(row) {
  reset();
  const _jcxmId = row.jcxmId || ids.value;
  await getJcxm(_jcxmId).then((response) => {
    form.value = response.data;
  });
  listMethod({ jcxmId: _jcxmId }).then((response) => {
    form.value.basicMethodList = response.data;
    form.value.basicMethodList.forEach((item) => {
      if (!item.isAuth) item.isAuth = 0;
      if (!item.isAgree) item.isAgree = 0;
      if (!item.isUse) item.isUse = 0;
    });
    heightBright.value = row.jcxmId;
    tableRowClass({ row });
  });
}

function transformation(row) {
  const a = proxy.getSupAndSub(row);
  return a;
}

getJclbTypeList();
getList();

function tabchange() {
  window.removeEventListener("resize", getTableHeight);
  nextTick(function () {
    getTableHeight();
    window.addEventListener("resize", getTableHeight);
  });
}

watch(
  () => total.value,
  () => {
    if (total.value) {
      heightParams.value = [
        { tableName: "main", bottomHeight: 74 },
        { tableName: "sub", bottomHeight: 24 },
      ];
    } else {
      heightParams.value = [
        { tableName: "main", bottomHeight: 21 },
        { tableName: "sub", bottomHeight: 24 },
      ];
    }
    getTableHeight();
  }
);

const heightParams = ref([
  { tableName: "main", bottomHeight: 21 },
  { tableName: "sub", bottomHeight: 24 },
]);
function getTableHeight() {
  proxy.adaptiveHeight(proxy, heightParams.value);
}

function submit() {
  let temp = {
    jcxmId: form.value.jcxmId,
    methodList: form.value.basicMethodList,
  };
  addJcxm(temp).then((res) => {
    proxy.$modal.msgSuccess(res.msg);
  });
}

function isOneChange(scope) {
  if (scope.row.isOne) {
    form.value.basicMethodList.forEach((item, index) => {
      form.value.basicMethodList[index].isOne = 0;
    });
    form.value.basicMethodList[scope.$index].isOne = 1;
  }
}

onMounted(() => {
  nextTick(() => {
    getTableHeight();
    window.addEventListener("resize", getTableHeight);
  })
})

onUpdated(() => {
  nextTick(function () {
    getTableHeight();
  });
});
onBeforeUnmount(() => {
  window.removeEventListener("resize", getTableHeight);
});

function tableRowClass({ row }) {
  if (row) {
    if (row.jcxmId == heightBright.value) {
      return "current-row";
    }
    return "";
  }
}

// const loadingBtn = ref(false);
// function handleSynchronous() {
//   proxy.$modal
//     .confirm("确认要同步检测项目吗？")
//     .then(() => {
//       postJcxm();
//       loadingBtn.value = true;
//       let timer = setInterval(() => {
//         getJcxmStatus()
//           .then((res) => {
//             if (res.data == 2) {
//               clearInterval(timer);
//               loadingBtn.value = false;
//               getList();
//               ElMessageBox.alert("同步成功", "提示", {
//                 type: "success",
//               });
//             }
//           })
//           .catch(() => {
//             clearInterval(timer);
//             loadingBtn.value = false;
//             ElMessageBox.alert("同步失败", "提示", {
//               type: "error",
//             });
//           });
//       }, 5000);
//     })
//     .catch(() => {});
// }
</script>

<style>
sub,
sup {
  font-size: 75%;
  line-height: 0;
  position: relative;
  vertical-align: baseline;
}

sup {
  top: -0.5em;
}

sub {
  bottom: -0.25em;
}
</style>
